/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-08-10
 * Time: 22:34
 */
public class Test {
    //移除数组元素
    public int removeElement(int[] nums, int val) {
        int slow = 0;
        int fast = 0;
        while(fast < nums.length) {
            if(nums[fast] != val) {
                nums[slow++] = nums[fast++];
            }else {
                fast++;
            }
        }

        return slow;
    }

    //x的平方根
    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if((long)mid * mid > x) {
                right = mid - 1;
            }else if((long)mid * mid < x) {
                left = mid + 1;
            }else {
                return mid;
            }
        }

        return left-1;
    }

    //有效的完全平方数
    public boolean isPerfectSquare(int num) {
        int left = 0;
        int right = num;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if((long)mid * mid > num) {
                right = mid - 1;
            }else if((long)mid * mid < num) {
                left = mid + 1;
            }else {
                return true;
            }
        }
        return false;
    }
}
